﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BO.Admin;
using BO;

namespace ProjectQuaDep.Admin.AwardsImages
{
    public partial class SetImage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShowGiaiThuong();
                ShowAll();
            }
        }
        protected void ShowAll()
        {
            string search = txtSearch.Text.Trim();
            string startDate = "";
            string endDate = "";
            string dkCount = "";
            string status = "2";
            string round = ddlRound.SelectedValue.ToString();
            string giaiID = ddlSetGiai.SelectedValue.ToString();
            DataTable dt = new DataTable();
            dt = ImagesBO.getAllImagesDoatGiaiBO(search, startDate, endDate, dkCount, status, round, giaiID);
            gvShow.DataSource = dt;
            gvShow.DataBind();
        }
        protected void ShowGiaiThuong()
        {
            string getRound = ddlRound.SelectedValue.ToString();
            DataTable dt = new DataTable();
            dt = ImagesBO.GetAllAwardsBO(getRound);
            ddlSetGiai.DataSource = dt;
            ddlSetGiai.DataValueField = Const.tbAwardsWeek.AwardsID;
            ddlSetGiai.DataTextField = Const.tbAwardsWeek.Title;
            ddlSetGiai.DataBind();
            lblPage.Text = "";
        }

        protected void cmdSearch_Click(object sender, ImageClickEventArgs e)
        {
            ShowAll();
        }

        protected void ddlRound_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowGiaiThuong();
            ShowAll();
        }

        protected void gvShow_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvShow.PageIndex = e.NewPageIndex;
            lblPage.Text = "Trang: " + (gvShow.PageIndex + 1).ToString();
            ShowAll();
        }

        protected void ddlSetGiai_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowAll();
        }

        protected void gvShow_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string cmdName = e.CommandName.ToString();
            string ID = e.CommandArgument.ToString();
            if (cmdName == "cmdDelete")
            {
                //xoa di
                ImagesBO.EditStatusAnhDoatGiaiBO(ID, "0");
            }
            else if (cmdName == "cmdCheck")
            {
                //xoa di
                ImagesBO.EditStatusAnhDoatGiaiBO(ID, "1");
            }
            ShowAll();
        }

        protected void CmdExcel_Click(object sender, EventArgs e)
        {
            SExcel();
        }
        public void SExcel()
        {
            try
            {
                DataTable dtE = new DataTable();
                string search = txtSearch.Text.Trim();
                string startDate = "";
                string endDate = "";
                string dkCount = "";
                string status = "2";
                string round = ddlRound.SelectedValue.ToString();
                string giaiID = ddlSetGiai.SelectedValue.ToString();
                dtE = ImagesBO.getAllImagesDoatGiaiBO(search, startDate, endDate, dkCount, status, round, giaiID);

                //string filename = "DownloadMobileNoExcel.xls";
                string filename = Server.MapPath("/Excel/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
                System.IO.StringWriter tw = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
                DataGrid dgGrid = new DataGrid();
                dgGrid.DataSource = dtE;
                dgGrid.DataBind();

                //Get the HTML for the control.
                dgGrid.RenderControl(hw);
                //Write the HTML back to the browser.
                //Response.ContentType = application/vnd.ms-excel;
                Response.ContentType = "application/vnd.ms-excel";
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
                Response.BufferOutput = true;
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                Response.Charset = "UTF-8";
                this.EnableViewState = false;
                Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
                Response.Write(tw.ToString());
                Response.End();
                //them duoi .xls vao
                //DataSet ds = new DataSet();
                //dt = BO.Admin.ImagesBO.GetInfomationImagesBO("108");
                //ds.Tables.Add(dt);
                //string filename = Server.MapPath("/Excel/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
                //ExcelLibrary.DataSetHelper.CreateWorkbook(filename, ds);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}